{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[56]\n",
      " [72]\n",
      " [69]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "matrix([[41.33509169],\n",
       "        [ 0.75458428]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np # type: ignore\n",
    "\n",
    "x = np.array([56, 72, 69, 88, 102, 86, 76, 79, 94, 74])\n",
    "y = np.array([92, 102, 86, 110, 130, 99, 96, 102, 105, 92])\n",
    "\n",
    "\n",
    "def least_squares_matrix(x: np.matrix, y: np.matrix):\n",
    "    \"\"\"最小二乘法矩阵求解\"\"\"\n",
    "    w = (x.T * x).I * x.T * y\n",
    "    return w\n",
    "\n",
    "x_matrix = np.matrix(np.hstack((np.ones((x.shape[0], 1)), x.reshape(x.shape[0], 1))))\n",
    "y_matrix = np.matrix(y.reshape(y.shape[0], 1))\n",
    "x_matrix, y_matrix\n",
    "\n",
    "least_squares_matrix(x_matrix, y_matrix)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
